package com.uuj.project.presenter;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.uuj.project.base.BasePresenter;
import com.uuj.project.base.BaseRequest;
import com.uuj.project.base.BaseResponse;
import com.uuj.project.listener.NetResultCallBack;
import com.uuj.project.net.BaseURL;
import com.uuj.project.net.CommonSubscriber;
import com.uuj.project.net.NetController;
import com.uuj.project.utils.RxBusUtil;
import com.uuj.project.view.IBaseView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import io.reactivex.subscribers.ResourceSubscriber;

/**
 * ProjectName:    ok_rxjava_retrofit
 * Package:        com.uuj.project.presenter
 * ClassName:      MainPresenter
 * Description:    描述
 * Author:         张继
 * CreateDate:     2019/8/8 15:09
 * UpdateUser:     更新者
 * UpdateDate:     2019/8/8 15:09
 * UpdateRemark:   更新说明
 * Version:        1.0
 */
public class MainPresenter extends BasePresenter<IBaseView> {
    public MainPresenter(IBaseView view) {
        super(view);
    }

    private int count = 0;
    public void getMsg() {

        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIzNzg1Njg3MTU4OTUwNDYxNDQiLCJpYXQiOjE1NzEzMDM5OTN9.xiJjuLqDTujtcTzWx6FyYq7LVf-Z2Tv10dH6BwlmmSY";
        String uid = "378568715895046144";
        String json = "BQq92ONHxT+GYOWZ/ccmBlFs7mrNXycLOwJYU8m+Z/936+FF75FwTKjVR5VRpWSJ";
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("token", token);
        Disposable disposable = NetController.getInstance().netServerDelay(String.class, getService().info(map, json), (NetResultCallBack<BaseResponse<Object>>) baseResponse -> {
            if (isViewAttach()) {
                ++count;
                ToastUtils.showLong((String)baseResponse.getData());
                if (count == 3) {
                    NetController.getInstance().unSubscribe(String.class);
                }
            }
        });
        NetController.getInstance().addSubscription(String.class, disposable);
    }
    //{"id":"378568715895046144","token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIzNzg1Njg3MTU4OTUwNDYxNDQiLCJpYXQiOjE1NzEzMDMwMTB9.-UnLk-EWZdHs154H2iUwC7oHJ-yqhyNid3wX942mkx4","sKey":"5F24718589611A716DD30C14A06C097D","cpyId":"355771576337039361","goDep":0}
    public void getMsg1() {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIzNzg1Njg3MTU4OTUwNDYxNDQiLCJpYXQiOjE1NzEzMDM5OTN9.xiJjuLqDTujtcTzWx6FyYq7LVf-Z2Tv10dH6BwlmmSY";
        String uid = "378568715895046144";
        String json = "BQq92ONHxT+GYOWZ/ccmBlFs7mrNXycLOwJYU8m+Z/936+FF75FwTKjVR5VRpWSJ";
        Map<String, String> map = new HashMap<>();
        map.put("uid", uid);
        map.put("token", token);
        subscription(MainPresenter.class, getService().info(map, json), new CommonSubscriber<Object>(getView()) {
            @Override
            public void onNext(Object o) {
                LogUtils.eTag(BaseURL.LOG,(String)o);
                NetController.getInstance().unSubscribe(MainPresenter.class);
                getView().hiddenDialog();
            }
        });

    }


    @Override
    public void detachView() {
        super.detachView();
        // 断开网络连接
        NetController.getInstance().unSubscribe(String.class);
        // 断开信息传递
        RxBusUtil.getInstance().unSubscribe(BaseResponse.class);
    }
}
